package hanjia;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/11-下午3:04
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode110 {


    public static boolean isBalanced(TreeNode root) {

        if (root==null||root.left==null&&root.right==null){
            return true;
        }

        boolean left=isBalanced(root.left);
        boolean right=isBalanced(root.right);
        if (left&&right){
            int leftDept = findDept(root.left);
            int rightDept = findDept(root.right);
            if (Math.abs(leftDept-rightDept)<=1){
                return true;
            }else {
                return false;
            }


        }
        return false;




    }


    public static int findDept(TreeNode root){

        if (root==null){
            return 0;
        }


        return Math.max(findDept(root.left),findDept(root.right))+1;

    }


        public static TreeNode constructTree() {
            TreeNode root = new TreeNode(3);
            TreeNode node9 = new TreeNode(9);
            TreeNode node20 = new TreeNode(20);
            TreeNode node15 = new TreeNode(15);
            TreeNode node7 = new TreeNode(7);
            root.left = node9;
            root.right = node20;
            node20.left = node15;
            node20.right = node7;
            return root;
        }


        public static void main(String[] args) {
            TreeNode root = constructTree();
            isBalanced(root);

        }
}
